package com.xshbbs.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xshbbs.entity.Partition;
import com.xshbbs.entity.User;
import com.xshbbs.service.PartitionService;

@Controller
public class PartitionController {
	
	@Autowired
	private PartitionService partitionService;
	
	@RequestMapping("insertPartition") 
	public String insertPartition(Partition partition, HttpServletRequest request) {
		
		User user = (User) request.getSession().getAttribute("user");
		if(user.getRole() != 2) {
			request.setAttribute("msg", "你没有权限");
			return "forum";
		}
		String admit = this.partitionService.admit(partition);
		if(!admit.equals("1")) {
			request.setAttribute("msg", admit);
			return "forum";
		}
		boolean result = this.partitionService.insertPartition(partition);
		if(result) {
			return "redirect:forum";
		}
		request.setAttribute("msg", "分区添加失败");
		return "forum";
		
	}
	
	@RequestMapping("deletePartition")
	public String deletePartition(int id, HttpServletRequest request) {
		
		User user = (User) request.getSession().getAttribute("user");
		if(user.getRole() != 2) {
			request.setAttribute("msg", "你没有权限");
			return "forum";
		}
		boolean result = this.partitionService.deletePartitionById(id);
		if(result) {
			return "redirect:forum";
		}
		request.setAttribute("msg", "删除分区失败");
		return "forum";
		
	}
	
	@RequestMapping("updatePartition")
	public String updatePartition(Partition partition, HttpServletRequest request) {
		
		User user = (User) request.getSession().getAttribute("user");
		if(user.getRole() != 2) {
			request.setAttribute("msg", "你没有权限");
			return "forum";
		}
		String admit = this.partitionService.admit(partition);
		if(!admit.equals("1")) {
			request.setAttribute("msg", admit);
			return "forum";
		}
		boolean result = this.partitionService.updatePartitionById(partition);
		if(result) {
			return "redirect:forum";
		}
		request.setAttribute("msg", "删除分区失败");
		return "forum";
		
	}
	
}
